package b2.widget.tank
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.base.*;
	import b2.scene.camp.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class TankDialog extends Sprite
	{
		//坦克槽
		public var tankSlot:TankSlot;
		//装备槽
		private var equipSlots:Array;
		private static var instance:TankDialog;
		//内容容器（出战按钮、名字）
		private var contentContainer:Sprite;
		//是否显示帮助
		public var guideFlag:Boolean;
		//帮助容器
		private var guideContainter:Sprite;
		//帮助步骤
		private var guideStep:int;
		//计时器
		private var timer:Timer;
		
		public function TankDialog()
		{
			var TankBgr:Class = ResourceHelper.getInstance().getClassByName("TankDialogBgr");
			var backgroundImage:Sprite = new TankBgr();
			addChild(backgroundImage);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "坦克";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			closeButton.x = this.width - 42;
			closeButton.y = 12;
			addChild(closeButton);
			//坦克展示
			tankSlot = new TankSlot();
			tankSlot.x = 90;
			tankSlot.y = 60;
			addChild(tankSlot);
			//装备槽
			//左侧
			equipSlots = new Array();
			var equipSlot:EquipSlot = new EquipSlot(0);
			equipSlot.x = 30;
			equipSlot.y = 70;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(1);
			equipSlot.x = 30;
			equipSlot.y = 140;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(2);
			equipSlot.x = 30;
			equipSlot.y = 210;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			//右侧
			equipSlot = new EquipSlot(3);
			equipSlot.x = 270;
			equipSlot.y = 70;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(4);
			equipSlot.x = 270;
			equipSlot.y = 140;
			addChild(equipSlot);  
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(5);
			equipSlot.x = 270;
			equipSlot.y = 210;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			//下方
			equipSlot = new EquipSlot(6);
			equipSlot.x = 30;
			equipSlot.y = 285;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(7);
			equipSlot.x = 110;
			equipSlot.y = 285;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(8);
			equipSlot.x = 190;
			equipSlot.y = 285;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			equipSlot = new EquipSlot(9);
			equipSlot.x = 270;
			equipSlot.y = 285;
			addChild(equipSlot);
			equipSlots.push(equipSlot);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			if (guideFlag)
			{
				guideStep = 0;
				timer = new Timer(5000);
				timer.addEventListener(TimerEvent.TIMER, onGuideTick);
				timer.start();
			}
		}

		private function onRemovedFromStage(event:Event):void
		{
        	if (timer != null)
        	{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onGuideTick);
        	}
			timer = null;
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
		}
		
		
		public static function getInstance():TankDialog
		{
			if (!instance)
				instance = new TankDialog();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			if (!parentScene.contains(this))
			{
				var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
				fresh(me.campTankId);
				this.x = 40;
				this.y = 100;
				parentScene.addChild(this);
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				//关闭的时候同步战力
				var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
				var campTank:TankInfo = PlayerInfoManager.getInstance().tanks[me.campTankId];
				ServerHelper.getInstance().syncFightPoint(campTank.fightPoint);
				this.parent.removeChild(this);
			}
		}
		
		private function onGuideTick(event:Event):void
		{
			if (guideContainter != null && guideContainter.parent != null)
				guideContainter.parent.removeChild(guideContainter);
			guideContainter = new Sprite();
			addChild(guideContainter);
			var blinkRect:BlinkRec;
			var bubbleMessage:BubbleMessage;
			if (guideStep == 0)
			{
				blinkRect = new BlinkRec(366, 100, 328, 249);
				bubbleMessage = new BubbleMessage("双击需要装备的物品");
				bubbleMessage.x = 368;
				bubbleMessage.y = 30;
			}
			else if (guideStep == 1)
			{
				blinkRect = new BlinkRec(25, 62, 62, 212);
				var blinkRect2:BlinkRec = new BlinkRec(264, 62, 62, 212);
				guideContainter.addChild(blinkRect2);
				bubbleMessage = new BubbleMessage("属性装备槽位", true);
				bubbleMessage.x = 88;
				bubbleMessage.y = 12;
			}
			else
			{
				blinkRect = new BlinkRec(23, 278, 303, 66);
				bubbleMessage = new BubbleMessage("特殊武器槽位", true);
				bubbleMessage.x = 310;
				bubbleMessage.y = 227;
			}
			guideContainter.addChild(blinkRect);
			guideContainter.addChild(bubbleMessage);
			guideStep = (guideStep + 1) % 3;
		}
		
		public function fresh(tankId:int):void
		{
			tankSlot.loadTank(tankId);
			var tank:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[tank.ownerId];
			for (var i:int = 0; i < 10; i ++)
			{
				var equipSlot:EquipSlot = equipSlots[i];
				equipSlot.refresh(); 
				if (equipSlot.isEquiped)
					equipSlot.removeEquip();
			}
			for (var comIndex:String in owner.objPackage.objectList)
			{
				var comIndexArr:Array = comIndex.split("_");
				if (parseInt(comIndexArr[3]) == tankId)
				{
					if (tank.ownerId == PlayerInfoManager.getInstance().myUserId)
						addEquip(comIndex, 2);
					else
						addEquip(comIndex, -1);
				}
			}
			addEquip(tank.getEquipByPosition(5), -1);
			addEquip(tank.getEquipByPosition(9), -1);
			if (contentContainer != null && contentContainer.parent != null)
				contentContainer.parent.removeChild(contentContainer);
			contentContainer = new Sprite();
			addChild(contentContainer);
			if (owner.id == PlayerInfoManager.getInstance().myUserId)
			{
				//出战按钮
				var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
				var campButton:BasicButton = new BasicButton(ButtonBgr, "出战");
				campButton.x = 125;
				campButton.y = 226;
				contentContainer.addChild(campButton);
				campButton.addEventListener(MouseEvent.CLICK, onCampButtonClick);
				if (owner.campTankId == tankId)
					campButton.disable = true;
				else
					campButton.disable = false;
			}
			else
			{
				var nameText:TextField = new TextField();
				nameText.selectable = false;
	        	nameText.autoSize = TextFieldAutoSize.CENTER;
	        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
	        	titleFomat.letterSpacing = 5;
				nameText.defaultTextFormat = titleFomat;
				nameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				nameText.text = owner.nickname;
				nameText.x = (this.width - nameText.width) / 2;
				nameText.y = 232;
				contentContainer.addChild(nameText);
			}
		}
		
		private function addEquip(comIndex:String, location:int):void
		{
			if (comIndex == null)
				return;
			var comIndexArr:Array = comIndex.split("_");
			var position:int = (Config.objectConfigs[comIndexArr[0]] as EquipConfig).position;
			(this.equipSlots[position] as EquipSlot).loadEquip(comIndex, location);
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
			Warehouse.getInstance().close();
		}
		
		private function onCampButtonClick(event:MouseEvent):void
		{
			var campButton:BasicButton = event.currentTarget as BasicButton;
			if (!campButton.disable)
			{
				ServerHelper.getInstance().changeCampTank(tankSlot.tankId);
				ServerHelper.getInstance().addEventListener(ServerEvent.CHANGE_CAMP_TANK, onChangeCampTankComplete);
				campButton.disable = true;
			}
		}
		
		private function onChangeCampTankComplete(event:ServerEvent):void
		{
			var popMessage:PopMessage = new PopMessage("更换出战坦克成功！");
			WidgetManager.getInstance().popOut([popMessage], false, true);
			Warehouse.getInstance().refresh();
			BaseScene.getInstance().playerPicIcon.refresh();
		}
	}
}